package com.yuyou.fn.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.service.ISiteConfigService;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.educational.entity.Campus;
import com.yuyou.fn.educational.entity.DelayChangeTeacher;
import com.yuyou.fn.educational.entity.Room;
import com.yuyou.fn.educational.entity.Teacher;
import com.yuyou.fn.educational.service.ICampusService;
import com.yuyou.fn.educational.service.IDelayChangeTeacherService;
import com.yuyou.fn.educational.service.IRoomService;
import com.yuyou.fn.educational.service.ITeacherService;
import com.yuyou.fn.educational.vo.*;
import com.yuyou.fn.platform.permission.DataPermission;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

/**
 * Created by wens on 16-11-29.
 */
@Controller
@RequestMapping("/api/admin/teaching/resource")
public class AdminTeachingResourceApiController extends ApiBaseController {

    private final static Logger log = LoggerFactory.getLogger(AdminTeachingResourceApiController.class);

    @Resource
    private ITeacherService teacherService ;

    @Resource
    private ICampusService campusService ;

    @Resource
    private IRoomService roomService ;

    @Resource
    private IDelayChangeTeacherService delayChangeTeacherService;

    @Resource
    private ISiteConfigService siteConfigService ;


	@ApiOperation("老师标签")
    @RequestMapping("/teacherTags")
    @ResponseBody
    public ReturnResult<String[]> teacherTags() {
        String teacherTags = siteConfigService.getConfigValueFromCache("teacher_tags");
        return  ReturnResult.success(teacherTags.split(","));
    }


	@ApiOperation("老师列表")
    @RequestMapping(value = "/teacherList" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Page<Teacher>> teacherList(TeacherQueryVo teacherQueryVo , @RequestParam(value = "pageNo" ,defaultValue = "1") int pageNo, @RequestParam(value = "pageSize" ,defaultValue = "20") int pageSize, HttpServletRequest request ) {
        DataPermission dataPermission = getDataPermission(request);
        Page<Teacher> page = teacherService.findTeacherByQueryForPage(teacherQueryVo,dataPermission, pageNo, pageSize);
        return ReturnResult.success(page);
    }

	@ApiOperation("老师详情")
    @RequestMapping(value = "/teacherDetail" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Teacher> teacherDetail(@RequestParam("teacherId") Long teacherId) {
        Teacher teacher = teacherService.findById(teacherId);
        return ReturnResult.success(teacher);
    }

	@ApiOperation("保存更新老师")
    @RequestMapping(value = "/saveOrUpdateTeacher" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> saveOrUpdateTeacher(TeacherSaveOrUpdateVo teacherSaveOrUpdateVo ) {
        teacherService.saveOrUpdateTeacher(teacherSaveOrUpdateVo);
        return ReturnResult.success("Ok");
    }

	@ApiOperation("删除老师")
    @RequestMapping(value = "/delTeacher" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> delTeacher(@RequestParam("teacherId") Long teacherId ) {
        teacherService.deleteTeacher(teacherId);
        return ReturnResult.success("Ok");
    }



    @ApiOperation("校区管理列表")
    @RequestMapping(value = "/campusManageList" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Page<Campus>> campusManageList(CampusQueryVo campusQueryVo , @RequestParam(value = "pageNo" ,defaultValue = "1") int pageNo, @RequestParam(value = "pageSize" ,defaultValue = "20") int pageSize, HttpServletRequest request ) {
        DataPermission dataPermission = getDataPermission(request);
        Page<Campus> page = campusService.findCampusByQueryForManage(campusQueryVo,dataPermission, pageNo, pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("校区使用列表")
    @RequestMapping(value = "/campusUseList" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Page<Campus>> campusListForAreaTeam(CampusQueryVo campusQueryVo , @RequestParam(value = "pageNo" ,defaultValue = "1") int pageNo, @RequestParam(value = "pageSize" ,defaultValue = "20") int pageSize, HttpServletRequest request ) {
        Page<Campus> page = campusService.findCampusByQueryForUse(campusQueryVo,getDataPermission(request) , pageNo, pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("区域校区列表")
    @RequestMapping(value = "/campusOfAreaTeam" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<List<Campus>> campusOfAreaTeam(@RequestParam("areaTeamId") Long areaTeamId , HttpServletRequest request ) {
        List<Campus> list = campusService.findCampusByAreaTeamId(areaTeamId);
        return ReturnResult.success(list);
    }

	@ApiOperation("校区详情")
    @RequestMapping(value = "/campusDetail" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Teacher> campusDetail(@RequestParam("campusId") Long campusId) {
        Campus campus = campusService.findById(campusId);
        return ReturnResult.success(campus);
    }

	@ApiOperation("保存更新校区")
    @RequestMapping(value = "/saveOrUpdateCampus" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> saveOrUpdateCampus(CampusSaveOrUpdateVo campusSaveOrUpdateVo ) {
        campusService.saveOrUpdateCampus(campusSaveOrUpdateVo);
        return ReturnResult.success("Ok");
    }

	@ApiOperation("删除校区")
    @RequestMapping(value = "/delCampus" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> delCampus(@RequestParam("campusId") Long campusId ) {
        campusService.deleteCampus(campusId);
        return ReturnResult.success("Ok");
    }


	@ApiOperation("教室列表")
    @RequestMapping(value = "/roomList" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Page<Room>> roomList(RoomQueryVo roomQueryVo , @RequestParam(value = "pageNo" ,defaultValue = "1") int pageNo, @RequestParam(value = "pageSize" ,defaultValue = "20") int pageSize, HttpServletRequest request ) {

        if(roomQueryVo.getCampusId() == null ){
            throw new IllegalArgumentException("Invalid campusId") ;
        }

        Page<Room> page = roomService.findRoomByQueryForPage(roomQueryVo, pageNo, pageSize);
        return ReturnResult.success(page);
    }

	@ApiOperation("教室详情")
    @RequestMapping(value = "/roomDetail" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Room> roomDetail(@RequestParam("roomId") Long roomId) {
        Room room = roomService.findById(roomId);
        return ReturnResult.success(room);
    }

	@ApiOperation("保存更新教室")
    @RequestMapping(value = "/saveOrUpdateRoom" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> saveOrUpdateRoom(RoomSaveOrUpdateVo roomSaveOrUpdateVo ) {
        roomService.saveOrUpdateRoom(roomSaveOrUpdateVo);
        return ReturnResult.success("Ok");
    }

	@ApiOperation("删除教室")
    @RequestMapping(value = "/delRoom" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> delRoom(@RequestParam("roomId") Long roomId ) {
        roomService.deleteRoom(roomId);
        return ReturnResult.success("Ok");
    }

	@ApiOperation("基础校区校区列表")
    @RequestMapping(value = "/baseCampusList" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<List<Campus>> baseCampusList(CampusQueryVo campusQueryVo, HttpServletRequest request ) {
        campusQueryVo.setState(1);
	    Page<Campus> page = campusService.findCampusByQueryForUse( campusQueryVo ,getDataPermission(request), 0, 5000);
        return ReturnResult.success(page.getRecords());
    }

	@ApiOperation("导出老师列表")
	@RequestMapping(value = "/exportTeachers", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public ReturnResult<Void> exportTeachers(TeacherQueryVo teacherQueryVo, HttpServletRequest request,
			HttpServletResponse response) {

		DataPermission dataPermission = getDataPermission(request);
		response.reset();
		response.setContentType("application/msexcel");
		response.setHeader("Content-Disposition","attachment;Filename=" + "teachers" + TimeUtils.date2String(new Date(), "yyyyMMddHHmmss") + ".xlsx");
		response.addHeader("Access-Control-Allow-Origin", "*");
		OutputStream os = null;
		Workbook excel = teacherService.exportTeachers(teacherQueryVo, dataPermission);
		try {
			os = response.getOutputStream();
			excel.write(os);
		} catch (IOException e1) {
			log.error("老师信息导出失败",e1);
			return ReturnResult.fail("fail");
		}finally {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return ReturnResult.success("Ok");
	}

    @ApiOperation("保存预设")
    @RequestMapping(value = "/saveDelayUpdateTeacher" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Page<Teacher>> saveDelayUpdateTeacher(@RequestParam("teacherId")long teacherId, @RequestParam("delayChangeTeacherVos")String delayChangeTeacherVos, HttpServletRequest request ) {
        List<DelayChangeTeacherVo> delayChangeTeacherVoList= JSONObject.parseArray(delayChangeTeacherVos,DelayChangeTeacherVo.class);
	    delayChangeTeacherService.saveDelayUpdateTeacher(teacherId,delayChangeTeacherVoList);
        return ReturnResult.success("ok");
    }

    @ApiOperation("预设信息")
    @RequestMapping(value = "/delayTeacherMessage" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<List<DelayChangeTeacher>> delayTeacherMessage(@RequestParam("teacherId")long teacherId, HttpServletRequest request ) {
        List<DelayChangeTeacher> delayChangeTeacherList = delayChangeTeacherService.findByTeacherIdStatus(teacherId, 0);
        return ReturnResult.success(delayChangeTeacherList);
    }


}
